package com.shiguo365.market.display.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shiguo365.market.R;
import com.shiguo365.market.bean.Product;

import java.util.List;

/**
 * Created by caomenglong on 16/7/5.
 */

public class ProductAdapter extends ArrayAdapter<Product>{

    private int resourceId;

    public ProductAdapter(Context context, int textViewResourceId, List<Product> objects) {
        super(context, textViewResourceId,objects);
        resourceId =textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Product product=getItem(position);
        View view;
        ViewHolder viewHolder;

        if(convertView == null){
            view =LayoutInflater.from(getContext()).inflate(resourceId,null);
            viewHolder=new ViewHolder();
            viewHolder.productImage=(ImageView) view.findViewById(R.id.product_imageview);
            viewHolder.productName= (TextView) view.findViewById(R.id.product_name);
            viewHolder.productCentext=(TextView) view.findViewById(R.id.product_context);
            viewHolder.productPrice =(TextView) view.findViewById(R.id.product_price);
            view.setTag(viewHolder);
        }else {
            view =convertView;
            viewHolder=(ViewHolder) view.getTag();
        }

        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(product.getImgurl(), viewHolder.productImage);

        viewHolder.productName.setText(product.getName());
        viewHolder.productCentext.setText(product.getContext());
        viewHolder.productPrice.setText("￥"+Double.toString(product.getPrice()));
        return view;
    }


    class ViewHolder{
        ImageView productImage;
        TextView productName;
        TextView productCentext;
        TextView productPrice;
    }



}
